package cn.edu.scau.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.validation.constraints.Min;

import cn.edu.scau.entity.Customer;
import cn.edu.scau.entity.Nurse;
import cn.edu.scau.service.CustomerService;
import cn.edu.scau.service.NurseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.edu.scau.bean.ResultBean;
import cn.edu.scau.dao.NursingRecordMapper;
import cn.edu.scau.entity.NursingRecord;
import cn.edu.scau.entity.NursingRecordShow;
import cn.edu.scau.service.NursingRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression.DateTime;

@Api(tags = "护理记录信息控制器")
@RestController
@RequestMapping("nursingRecord")
public class NursingRecordController {
	@Autowired
	private NursingRecordService nursingRecordService;
	@Autowired
	private NurseService nurseService;
	@Autowired
	private CustomerService customerService;


	/**
	  * 查询所有护理记录信息 
	 * @return
	 */
	@ApiOperation(value = "返回所有护理记录信息")
	@GetMapping(value = "/list")
	public ResultBean list(@RequestParam(value = "pageNum",defaultValue = "1")int pageNum,
			@RequestParam(value = "pageSize",defaultValue = "5")int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<NursingRecordShow> nursingRecordShows=nursingRecordService.selectAll();
		PageInfo<NursingRecordShow> pageInfo = new PageInfo<NursingRecordShow>(nursingRecordShows,pageSize);
		ResultBean resultBean =new ResultBean(200,true,"查询所有护理记录信息成功~~~",pageInfo);
		return resultBean;
	}
	

	/**
	 * 根据时间段查询
	 * @param preTime
	 * @param nextTime
	 * @param pageNum
	 * @param pageSize
	 * @return
	 * @throws ParseException 
	 */
	@ApiOperation(value = "根据时间段查询")
	@ApiImplicitParams(value = {
			@ApiImplicitParam(paramType = "query",name="preTime",value = "起始时间",dataType = "Date"),
			@ApiImplicitParam(paramType = "query",name="nextTime",value = "终止时间",dataType = "Date"),
	})
	@GetMapping(value = "/find")
	public ResultBean find(String preTime,String nextTime,
			@RequestParam(value = "pageNum",defaultValue = "1")int pageNum,
			@RequestParam(value = "pageSize",defaultValue = "5")int pageSize) throws ParseException {
		PageHelper.startPage(pageNum, pageSize);
		List<NursingRecordShow> nursingRecordShows=nursingRecordService.findByPeriod(new Date(Long.valueOf(preTime)),new Date(Long.valueOf(nextTime)));
		PageInfo<NursingRecordShow> pageInfo = new PageInfo<NursingRecordShow>(nursingRecordShows);
		ResultBean r =new ResultBean(200,true,"模糊查询成功~~~",pageInfo);
		return r;
	}
	
	
	/**
	 * 新增护理记录
	 * @param
	 * @param
	 * @return
	 */
	@PostMapping(value = "/insert")
	public ResultBean insert(Integer nursingcontentid, Integer customerid, Integer nurseid,
			String nursingrecordtime)  {
		Date date = new Date(Long.valueOf(nursingrecordtime));
		NursingRecord nursingRecord = new NursingRecord().builder()
				.nursingcontentid(nursingcontentid)
				.customerid(customerid)
				.nurseid(nurseid)
				.nursingrecordtime(date)
				.build();
		ResultBean resultBean =null;
		Customer customer = customerService.selectByPrimaryKey(customerid);
		Nurse nurse = nurseService.selectByPrimaryKey(nurseid);
		if(customer!=null&&customer.getCustomeruseflag()) {
			if(nurse!=null&&nurse.getNurseuseflag()) {
				boolean flag = nursingRecordService.insert(nursingRecord);
				if (flag) {
					resultBean = new ResultBean(200, true, "新增护理记录信息成功~~~\n", null);
				} else {
					resultBean = new ResultBean(5005, false, "新增护理记录信息失败", null);
				}
			}
			else{
				resultBean = new ResultBean(5005, false, "没有当前编号护士", null);
			}
		}else
		{
			resultBean = new ResultBean(5005, false, "没有当前编号客户", null);
		}
		return resultBean;
	}

	/**
	 * 根据编号查询护理记录信息
	 * @param nursingrecordid
	 * @return
	 */
	@ApiOperation(value = "根据护理记录编号查询信息")
	@ApiImplicitParams(value = {
			@ApiImplicitParam(paramType = "path",name = "nursingrecordid",value = "护理记录编号",required = true,dataType = "int")
	})
	@GetMapping(value = "/{nursingrecordid}")
	public ResultBean detail(
			@PathVariable("nursingrecordid")Integer nursingrecordid) {
		NursingRecord nursingRecord = nursingRecordService.selectById(nursingrecordid);
		ResultBean resultBean=new ResultBean(200,true,"查询成功~~~",nursingRecord);
		return resultBean;
	}
	
	/**
	  * 修改护理记录信息
	 * @param
	 * @param
	 * @param
	 * @return
	 */
	@PutMapping(value = "/update/{nursingrecordid}")
	public ResultBean update(
			@PathVariable("nursingrecordid")Integer nursingrecordid,
			Integer nursingrecordID,Integer nursingcontentid, Integer customerid, Integer nurseid,String nursingrecordtime) {
		ResultBean resultBean =null;
		System.err.println(nursingrecordID);
		Date date = new Date(Long.valueOf(nursingrecordtime));
		NursingRecord nursingRecord = new NursingRecord().builder()
				.nursingrecordid(nursingrecordID)
				.nursingcontentid(nursingcontentid)
				.customerid(customerid)
				.nurseid(nurseid)
				.nursingrecordtime(date)
				.build();
		System.err.println(nursingRecord);
		Customer customer = customerService.selectByPrimaryKey(customerid);
		Nurse nurse = nurseService.selectByPrimaryKey(nurseid);
		if(customer!=null&&customer.getCustomeruseflag()) {
			if(nurse!=null&&nurse.getNurseuseflag()) {
			boolean flag = nursingRecordService.update(nursingRecord);
				if(flag) {
			resultBean = new ResultBean(200,true,"修改护理记录成功~~~\n",null);
					}else {
			resultBean = new ResultBean(5005,false,"修改护理记录失败",null);
					}
			}
			else{
				resultBean = new ResultBean(5005, false, "没有当前编号护士", null);
			}
		}else
		{
			resultBean = new ResultBean(5005, false, "没有当前编号客户", null);
		}
		return resultBean;
	}
	

}
